﻿using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace GKQC.DbHelper.Services
{
    public class LockService
    {
        static string lockString = "單據已被鎖定！\r\n鎖定人：{0}\r\n單據Id：{1}\r\n鎖定電腦：{2}\r\n鎖定時間：{3}";
        public static LockModel GetLockModel(AllDbContext dbContext,Form form,string pkValue, out string msg)
        {
            msg = string.Empty;
            LockModel lockModel = dbContext.QcDb.Queryable<LockModel>()
                .Where(l => l.FormCode == form.Name && l.PkValues == pkValue)
                .Single();
            if(lockModel != null)
                msg = string.Format(lockString, lockModel.UserName, lockModel.PkValues, lockModel.HostName, lockModel.StartTime.ToString("yyyy-MM-dd HH:mm:ss"));
            return lockModel;
        }

        public static List<LockModel> GetLockModels(AllDbContext dbContext, Form form, List<long> pkValues, out string msg)
        {
            msg = string.Empty;
            var lockModels = dbContext.QcDb.Queryable<LockModel>()
                .Where(l => l.FormCode == form.Name)
                .In(t => t.PkValues,pkValues);
            if(lockModels != null && lockModels.Count() >0)
                return lockModels.ToList();
            else 
                return null;
        }

        public static int Lock(AllDbContext dbContext, UserModel user,Form form, string pkValue,string billNo)
        {
            LockModel lockModel = new LockModel();
            lockModel.OnlineId = user.OnlineId;
            lockModel.FormCode = form.Name;
            lockModel.FormName = form.Text;
            lockModel.HostName = System.Environment.MachineName;
            lockModel.LoginTime = user.LoginServerTime.GetValueOrDefault();
            lockModel.StartTime = user.GetServerTime();
            lockModel.UserCode = user.UserCode;
            lockModel.UserName = user.UserName;
            lockModel.PkValues = pkValue;
            lockModel.BillNo = billNo;
            return dbContext.QcDb.Insertable<LockModel>(lockModel).ExecuteReturnIdentity();
        }

        public static void DelLock(AllDbContext dbContext, UserModel user, Form form, string pkValue)
        {
            dbContext.QcDb.Deleteable<LockModel>()
                .Where(d => d.OnlineId==user.OnlineId && d.FormCode == form.Name && d.PkValues == pkValue)
                .ExecuteCommand();
        }

        public static void DelAllLock(AllDbContext dbContext, UserModel user)
        {
            dbContext.QcDb.Deleteable<LockModel>()
                .Where(d => d.OnlineId == user.OnlineId)
                .ExecuteCommand();
        }

        public static void DelLock(AllDbContext dbContext, List<int> Ids)
        {
            dbContext.QcDb.Deleteable<LockModel>()
                .In(Ids)
                .ExecuteCommand();
        }
    }
}
